package com.pro.springbootpro.controller;

import com.power.common.util.UUIDUtil;
import com.pro.springbootpro.entity.SysUser;
import com.pro.springbootpro.entity.SysUserRole;
import com.pro.springbootpro.service.ISysUserRoleService;
import com.pro.springbootpro.service.ISysUserService;
import com.pro.springbootpro.utils.ResponseUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@Slf4j
@RestController
public class LoginController {

    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private ISysUserRoleService sysUserRoleService;

    /**
     * 注册普通用户
     *
     * @return
     */
    @Transactional
    @RequestMapping(value = "/registry",method = RequestMethod.POST)
    public ResponseUtils registry(@RequestBody Map<String,Object> paramMap) {
        log.info(paramMap.toString());
        SysUser sysUser = new SysUser();
        sysUser.setId(UUIDUtil.getUuid());
        sysUser.setUsername(paramMap.get("username").toString());
        sysUser.setPassword(bCryptPasswordEncoder.encode(paramMap.get("password").toString()));
        sysUser.setStatus("0");
        sysUserService.save(sysUser);

        // 注册普通用户
        SysUserRole sysUserRole = new SysUserRole();
        sysUserRole.setId(UUIDUtil.getUuid());
        sysUserRole.setUserId(sysUser.getId());
        sysUserRole.setRoleId(1);
        sysUserRoleService.save(sysUserRole);

        return ResponseUtils.success(sysUser);
    }

}
